package com.example.jacky.android_easeui.demo;


import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jacky.android_easeui.R;
import com.example.jacky.android_easeui.base.BaseActivity;
import com.example.jacky.android_easeui.bean.Person;
import com.example.jacky.android_easeui.bean.Cheeses;
import com.example.jacky.uilibrary.utils.Utils;
import com.example.jacky.uilibrary.view.Flowlayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
 * 标签容器
 */
public class TagViewActivity extends BaseActivity {
    public static final String TAG = "TagViewActivity";
    private ArrayList<Person> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_tagview);
        datas = new ArrayList<Person>();
        fillAndSortData(datas);
        initTags();
    }

    private void initTags() {
//        Flowlayout layout = new Flowlayout(this);
        Flowlayout layout = (Flowlayout) findViewById(R.id.fl);
        int padding = Utils.dip2px(13);
        layout.setPadding(padding, padding, padding, padding);
        //layout.setOrientation(LinearLayout.VERTICAL);// 设置线性布局的方向
        int backColor = 0xffcecece;
        Drawable pressedDrawable = Utils.createShape(backColor);// 按下显示的图片
        for (int i = 0; i < datas.size(); i++) {
            TextView textView = new TextView(this);
            final String str = datas.get(i).getName();
            textView.setText(str);

            Random random = new Random();   //创建随机
            int red = random.nextInt(200) + 22;
            int green = random.nextInt(200) + 22;
            int blue = random.nextInt(200) + 22;
            int color = Color.rgb(red, green, blue);//范围 0-255
            GradientDrawable createShape = Utils.createShape(color); // 默认显示的图片
            StateListDrawable createSelectorDrawable = Utils.createSelectorDrawable(pressedDrawable, createShape);// 创建状态选择器
            textView.setBackgroundDrawable(createSelectorDrawable);
            textView.setTextColor(Color.WHITE);
            //textView.setTextSize(UiUtils.dip2px(14));
            int textPaddingV = Utils.dip2px(4);
            int textPaddingH = Utils.dip2px(7);
            textView.setPadding(textPaddingH, textPaddingV, textPaddingH, textPaddingV); //设置padding
            textView.setClickable(true);//设置textView可以被点击
            textView.setOnClickListener(new View.OnClickListener() {  // 设置点击事件

                @Override
                public void onClick(View v) {
                    Utils.showToast(str);
                }
            });
            layout.addView(textView, new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, -2));// -2 包裹内容
        }
    }

    private void fillAndSortData(ArrayList<Person> persons) {
        // 填充数据
        for (int i = 0; i < Cheeses.NAMES.length; i++) {
            String name = Cheeses.NAMES[i];
            persons.add(new Person(name));
        }

        // 进行排序
        Collections.sort(persons);
    }
}
